<template>
  <el-dialog v-model="visible" :title="`${!dataForm.id ? $t('add') : $t('update')}编组信息`" :close-on-click-modal="false"
    :close-on-press-escape="false" width="600">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
      label-width="100px">
      <el-form-item :label="$t('device.groupName')" prop="name" required>
        <el-input v-model="dataForm.name" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item :label="$t('device.parent')" prop="pid">
        <el-cascader v-model="dataForm.pid" :options="deviceGroupList"
          :props="{ label: 'name', value: 'id', checkStrictly: true, emitPath: false }"
          style="width: 100%;"></el-cascader>
      </el-form-item>
      <el-form-item :label="$t('device.groupType')" prop="type" required>
        <el-radio-group v-model="dataForm.type">
          <el-radio :label="1">位置</el-radio>
          <el-radio :label="2">设备</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="$t('device.sort')" prop="sort" required>
        <el-input-number v-model="dataForm.sort" controls-position="right"></el-input-number>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
import { DEVICE_MANUFACTURER_OPTIONS, DEVICE_TYPE_OPTIONS, DEVICE_PURPOSE_OPTIONS } from './contant';
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  name: "",
  pid: null,
  sort: null,
  type: null,
});

const deviceGroupList = ref<any[]>([]);

const rules = ref({
  name: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  type: [{ required: true, message: t("validate.required"), trigger: "change" }],
  pid: [{ required: true, message: t("validate.required"), trigger: "change" }],
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  Promise.all([getDeviceGroupList()]).then(() => {
    if (id) {
      getInfo(id);
    }
  });
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/device/class/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 获取编组列表
const getDeviceGroupList = () => {
  baseService.get("/device/class/list").then((res) => {
    deviceGroupList.value = res.data;
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    baseService[!dataForm.id ? 'post' : 'put'](`/device/class`, dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
